/**
 * author:Administrator
 * date:2016/2/26
 * desc:TODO
 */

'use strict';
let dateformat = require('dateformat');

// 时间格式1 yyyy-mm-dd HH:MM:ss
// 时间格式2 linux 时间戳:距离1970-01-01 0:0:0 的秒数

let DEFAULT_DATE_FORMAT = 'yyyy-mm-dd HH:MM:ss';

class FormatDate {
  static unixTimestamp(date) {
    return parseInt(date || new Date().getTime() / 1000);
  }

  static readableTimeStamp(date) {
    return dateformat(date || new Date(), DEFAULT_DATE_FORMAT);
  }

  /**
   * unix 时间戳 => 可读
   * @param unixTimestamp
   * @returns {*}
   */
  static unix2Readable(unixTimestamp) {
    let ms = unixTimestamp * 1000;
    //console.log('unix2Readable, ms=%s', ms);
    return FormatDate.readableTimeStamp(new Date(ms));
  }

  /**
   * 可读 => unix 时间戳
   * @param readableTimestamp
   * @returns {*}
   */
  static readable2Unix(readableTimestamp) {
    let ms = new Date(readableTimestamp).getTime();
    return unixTimestamp(ms);
  }
}

module.exports = FormatDate;